package com.wonsec.useragent.utils;


import javax.servlet.http.HttpServletRequest;

/**
 * @Author： mzy
 * @Date： 2025/5/13 下午2:32
 * @Describe：
 */
public class ChannelUtils {

    public static String detectChannel(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        if (userAgent.contains("micromessenger")) {
            return "wechat";
        } else if (userAgent.contains("douyin") || userAgent.contains("toutiao")) {
            return "douyin";
        } else if (userAgent.contains("mozilla") || userAgent.contains("chrome")
                || userAgent.contains("safari") || userAgent.contains("edge")) {
            return "browser";
        } else {
            return "other";
        }
    }

    public static String detectChannelDetail(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent").toLowerCase();

        // 微信生态细分
        if (userAgent.contains("micromessenger")) {
            if (userAgent.contains("miniprogram")) {
                return "wechat_miniprogram"; // 微信小程序
            } else if (userAgent.contains("wxwork")) {
                return "wechat_work"; // 企业微信
            } else {
                return "wechat_public"; // 微信公众号
            }
        }
        // 抖音生态细分
        else if (userAgent.contains("douyin")) {
            if (userAgent.contains("lite")) {
                return "douyin_lite"; // 抖音极速版
            } else {
                return "douyin_app"; // 抖音主APP
            }
        }
        // 浏览器细分
        else if (userAgent.contains("mozilla") || userAgent.contains("chrome")
                || userAgent.contains("safari") || userAgent.contains("edge")) {
            if (userAgent.contains("mobile")) {
                return "mobile_browser"; // 手机浏览器
            } else if (userAgent.contains("micromessenger")) {
                return "wechat_browser"; // 微信内置浏览器
            } else {
                return "desktop_browser"; // PC浏览器
            }
        }

        return "unknown";
    }

    public static String getClientIp(HttpServletRequest request) {
        // 获取客户端真实IP（考虑代理情况）
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.split(",")[0]; // 处理多IP情况
    }
}
//public class ChannelUtils {
//
//    public static String detectChannel(HttpServletRequest request) {
//        String userAgent = request.getHeader("User-Agent").toLowerCase();
//        System.out.println(userAgent);
//        if (userAgent.contains("micromessenger")) {
//            return "wechat";
//        } else if (userAgent.contains("douyin") || userAgent.contains("toutiao")) {
//            return "douyin";
//        } else if (userAgent.contains("mozilla") || userAgent.contains("chrome")
//                || userAgent.contains("safari") || userAgent.contains("edge")) {
//            return "browser";
//        } else {
//            return "other";
//        }
//    }
//
//    public static String getClientIp(HttpServletRequest request) {
//        // 获取客户端真实IP（考虑代理情况）
//        String ip = request.getHeader("X-Forwarded-For");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//    }
//}
